<?php
namespace App\Financeiro\Controller;

use Foxforms\Widget\Field\Select;

use Foxforms\Widget\Field\Input;

use Foxforms\Widget\Form;

class bancoController extends \Foxframe\Controller
{
	public function viewListar($params = array())
	{
		$lista	= $this->em->getRepository('\Entities\Financeiro\Banco')->findAll();
		$this->view->assign('lista', $lista);
	}

	public function viewSalvarBanco($params = array())
	{
		if (isset($params['id']) && !empty($params['id']))
			$banco	= $this->em->find('\Entities\Financeiro\Banco', $params['id']);
		else
			$banco	= new \Entities\Financeiro\Banco();

		$form	= new Form();
		$form->addValues($banco, 'banco');
		$form->addElement(Input::create('application')->setType('hidden')->setValue('financeiro'));
		$form->addElement(Input::create('controller')->setType('hidden')->setValue('banco'));
		$form->addElement(Input::create('method')->setType('hidden')->setValue('saveSalvarBanco'));
		$form->addElement(Input::create('banco[idbanco]')->setType('hidden'));
		$form->addElement(Input::create('banco[cobanco]')->setType('text')->setLabel('Código')->validate()->addClass('input-mini'));
		$form->addElement(Input::create('banco[dsnome]')->setType('text')->setLabel('Nome')->validate()->addClass('input-xlarge'));
		$form->addElement(Input::create('salvar')->setType('submit')->setValue('Salvar')->addClass(array('btn','btn-primary')));
		$this->view->assign('form', $form);
		return $this->returnAjax();
	}

	public function saveSalvarBanco($params = array())
	{
		try
		{
			if (isset($params['banco']['idbanco']) && !empty($params['banco']['idbanco']))
				$banco	= $this->em->find('\Entities\Financeiro\Banco', $params['banco']['idbanco']);
			else
				$banco	= new \Entities\Financeiro\Banco();

			$banco->setNome($params['banco']['dsnome']);
			$banco->setCodigo($params['banco']['cobanco']);

			$this->em->persist($banco);
			$this->em->flush();

			$this->setFlash('Success', 'Sucesso', "Banco salvo com sucesso");
		}
		catch (\Exception $e)
		{
			$this->setFlash('Error', 'Erro', $e->getMessage());
		}
		$this->redirect($this->uri->getOrigem());
	}
}